package com.cqhilink.iot.jobstandardmanagement.services.webservice.impl;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.iot.jobstandardmanagement.common.thread.ThreadPoolUtil;
import com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.*;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.button.util.MenuButtonPool;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.button.util.RoleMenuButtonPool;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.employee.util.EmployeePool;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.util.OrganizationPool;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.orgarea.util.OrgUserPool;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.permission.util.AccountRolePool;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.permission.util.MenuPool;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.permission.util.RolesPool;
import com.cqhilink.iot.jobstandardmanagement.services.webservice.systemWebServices.SystemWebService;
import com.cqhilink.iot.jobstandardmanagement.services.webservice.util.SystemWebServicesStub;
import com.sun.jersey.api.client.Client;
import org.apache.commons.lang.StringUtils;

import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.List;

/**
 * @Author 卢曼成
 * @Description SystemWebService接口实现类
 * @Create 2017-02-05 9:23
 */
@org.springframework.stereotype.Service("systemWebService")
public class SystemWebServiceImpl implements SystemWebService {

    @Override
    public void interfaceNotification(Service service) {
        if(service == null){
            service = new Service();
        }
        Route r = service.getRoute();
        if(r == null){
            r = new Route();
        }
        Long time = System.currentTimeMillis();
        r.setRequesterStartTime(time.toString());//请求开始时间
        time = System.currentTimeMillis();
        r.setProviderStartTime(time.toString());//供应商开始时间
        r.setEnd_timestamp(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        r.setServiceNameSpace("http://soa.com");
        r.setSoapenv("http://schemas.xmlsoap.org/soap/envelope/");
        r.setIsBigData("0");
        r.setServiceTimeOut("60000");
        r.setReply_qmgr("ESB_QM11");
        ServiceResponse sr = new ServiceResponse();
        sr.setStatus("COMPLETE");
        r.setServiceResponse(sr);
        RequesterMQ rmq = new RequesterMQ();
        rmq.setResponseIN("EIS.QUEUE.RESPONSE.SRM");
        rmq.setResponseOUT("EIS.QUEUE.RESPONSE.OUT.SRM");
        r.setRequesterMQ(rmq);
        time = System.currentTimeMillis();
        r.setProviderEndTime(time.toString());//供应商结束时间
        time = System.currentTimeMillis();
        r.setRequesterEndTime(time.toString());//请求结束时间
        Data data = service.getData();
        if(data == null){
            data = new Data();
        }
        Response res = new Response();
        res.setReturn("成功");
        data.setResponse(res);

        this.getJurisdiction(service);

//		try {
//			this.getJurisdictions(service);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
    }

    @Override
    public void masterData(Service service) {
        if(service == null){
            service = new Service();
        }
        Route route = service.getRoute();
        if(route == null){
            route = new Route();
        }
        route.setSerialNO("1");
        route.setServiceID("08003000000002");
        Long time = System.currentTimeMillis();
        route.setServiceTime(time.toString());
        route.setSourceSysID("08001");
        Data data = service.getData();
        if(data == null){
            data = new Data();
        }
        Request request = new Request();
//        request.setArg0("organization");
//        request.setArg1("");
//        request.setArg2("");
        data.setRequest(request);
        service.setData(data);
        service.setRoute(route);
    }

    /**
     * @Author 卢曼成
     * @Description 系统数据获取处理
     * @Date 2017/2/5 9:26
     */
    private void getJurisdictions(Service service) throws Exception {
        Route route = service.getRoute();
        Data data = service.getData();

        if (route != null && data != null) {
            Request request = data.getRequest();
            if (request != null) {
                StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:impl=\"http://impl.webservcice.eis.com/\">");
                sb.append("<soapenv:Header/>");
                sb.append("<soapenv:Body>");
                sb.append("<impl:esbServiceOperation>");
                sb.append("<arg0>");
                sb.append("<Service>");
                sb.append("<Route>");
                sb.append("<SerialNO>" + route.getSerialNO() + "</SerialNO>");
                sb.append("<ServiceID>" + route.getServiceID() + "</ServiceID>");
                sb.append("<ServiceTime>" + route.getServiceTime() + "</ServiceTime>");
                sb.append("<SourceSysID>" + route.getSourceSysID() + "</SourceSysID>");
                sb.append("</Route>");
                sb.append("<Data>");
                sb.append("<Control></Control>");

                sb.append("<Request>");
                sb.append("<sysID>" + request.getSysID() + "</sysID>");
                sb.append("<type>" + request.getType() + "</type>");
                sb.append("<dataIDs>" + request.getDataIDs() + "</dataIDs>");
                sb.append("<dataType>" + request.getDataType() + "</dataType>");
                sb.append("</Request>");
                sb.append("</Data>");
                sb.append("</Service>");
                sb.append("</arg0>");
                sb.append("</impl:esbServiceOperation>");
                sb.append("</soapenv:Body>");
                sb.append("</soapenv:Envelope>");
//				String xml = HttpClientUtil.httpPostWithJson("http://10.89.1.103:7080/httpservice", sb.toString());
                String xml = Client.create().resource("http://10.89.1.103:7080/httpservice").type(MediaType.APPLICATION_XML).post(String.class
                        , sb.toString());
                if (xml.contains("<Response>")) {
                    String returns = xml.substring(xml.indexOf("<Response>") + 18, xml.lastIndexOf("</Response>") - 9);
                    returns = returns.replaceAll("&quot;", "\"");
                    service(request.getType(), returns, request.getControlType());
//                    List<com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.Organization> list = JSON.parseArray(returns, com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.Organization.class);
                }
            }
        }
    }

    /**
     * @Author 卢曼成
     * @Description 系统数据获取处理
     * @Date 2017/2/5 9:26
     */
    private void getJurisdiction(Service service) {
//        SystemWebServicesStub.Route swr = new SystemWebServicesStub.Route();
//        swr.setSerialNO("1");
//        swr.setServiceID("70201000000001");
//        swr.setServiceTime("1");
//        swr.setSourceSysID("60501");
//
//        SystemWebServicesStub.Request swreque = new SystemWebServicesStub.Request();
//        swreque.setSysID("60501");
//        Request request = service.getData().getRequest();
//        swreque.setType(request.getType());
//        swreque.setDataIDs(request.getDataIDs());
//        swreque.setControlType(request.getControlType());
//        swreque.setDataType("2");
//
//        SystemWebServicesStub.Data swd = new SystemWebServicesStub.Data();
//        //添加Request
//        swd.setRequest(swreque);
//
//        SystemWebServicesStub.Service ser = new SystemWebServicesStub.Service();
//        //添加Data
//        ser.setData(swd);
//        //添加Route
//        ser.setRoute(swr);
//        this.sendService(null);
    }

//    public void sendService(SystemWebServicesStub.Service service) {
//        try {
//            SystemWebServicesStub.GetJurisdiction sg = new SystemWebServicesStub.GetJurisdiction();
//            sg.setService(service);
//            SystemWebServicesStub s = new SystemWebServicesStub();
//            SystemWebServicesStub.Service ser = s.getJurisdiction(sg).get_return();
//            SystemWebServicesStub.Data data = ser.getData();
//            if (data != null && data.getResponse() != null && data.getRequest() != null && data.getResponse() != null) {
//                SystemWebServicesStub.Request request = data.getRequest();
//                SystemWebServicesStub.Response response = data.getResponse();
//                if (StringUtils.isNotEmpty(request.getType()) && StringUtils.isNotEmpty(response.get_return())) {
//                    service(request.getType(), response.get_return(), request.getControlType());
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            long end = System.currentTimeMillis();
//        }
//    }

    /**
     * 类型 1.组织机构, 2.组织机构扩展, 3.用户, 4.用户扩展, 5.应用系统, 6.菜单, 7.功能, 8.角色, 9.菜单功能关系, 10.用户角色关系, 11.角色权限, 12.组织机构用户关系, 13.组织机构用户权限关系
     *
     * @param type
     * @param returns
     * @throws Exception
     */
    private void service(String type, String returns, String controlType) throws Exception {
        if ("1".equals(type)) {
            List<UasOrg> list = JSON.parseArray(returns, UasOrg.class);
            OrganizationPool param = new OrganizationPool();
            param.setControlType(controlType);
            param.setType(type);
            param.setUoList(list);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("2".equals(type)) {
            List<UasOrgExt> list = JSON.parseArray(returns, UasOrgExt.class);
            OrganizationPool param = new OrganizationPool();
            param.setType(type);
            param.setControlType(controlType);
            param.setUoeList(list);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("3".equals(type)) {
            List<UasUser> list = JSON.parseArray(returns, UasUser.class);
            EmployeePool param = new EmployeePool();
            param.setUuList(list);
            param.setControlType(controlType);
            param.setType(type);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("4".equals(type)) {
            List<UasUserExt> list = JSON.parseArray(returns, UasUserExt.class);
            EmployeePool param = new EmployeePool();
            param.setType(type);
            param.setUueList(list);
            param.setControlType(controlType);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("5".equals(type)) {
//            List<UasApp> list = JSON.parseArray(returns, UasApp.class);
//            SystemPlatformPool param = new SystemPlatformPool();
//            param.setControlType(controlType);
//            param.setList(list);
//            ThreadPoolUtil.getInit().execute(param);
        } else if ("6".equals(type)) {
            List<UasMenu> list = JSON.parseArray(returns, UasMenu.class);
            MenuPool param = new MenuPool();
            param.setList(list);
            param.setControlType(controlType);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("7".equals(type)) {
//            List<UasFunc> list = JSON.parseArray(returns, UasFunc.class);
//            ButtonPool param = new ButtonPool();
//            param.setControlType(controlType);
//            param.setList(list);
//            ThreadPoolUtil.getInit().execute(param);
        } else if ("8".equals(type)) {
            List<UasRole> list = JSON.parseArray(returns, UasRole.class);
            RolesPool param = new RolesPool();
            param.setControlType(controlType);
            param.setList(list);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("9".equals(type)) {
            List<UasMenuFunc> list = JSON.parseArray(returns, UasMenuFunc.class);
            MenuButtonPool param = new MenuButtonPool();
            param.setList(list);
            param.setControlType(controlType);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("10".equals(type)) {
            List<UasUserRole> list = JSON.parseArray(returns, UasUserRole.class);
            AccountRolePool param = new AccountRolePool();
            param.setList(list);
            param.setControlType(controlType);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("11".equals(type)) {
            List<UasRoleRes> list = JSON.parseArray(returns, UasRoleRes.class);
            RoleMenuButtonPool param = new RoleMenuButtonPool();
            param.setControlType(controlType);
            param.setList(list);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("12".equals(type)) {
            List<UasOrgUser> list = JSON.parseArray(returns, UasOrgUser.class);
            OrgUserPool param = new OrgUserPool();
            param.setControlType(controlType);
            param.setList(list);
            ThreadPoolUtil.getInit().execute(param);
        } else if ("13".equals(type)) {

        }
    }


}
